//
//  SEFilterHeader.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/6.
//

import UIKit

enum SEFilterHeaderFilterType: Int {
    case left, center, right, unknown = -999
    
    var menuStyle: SEFilterHeaderMenuViewStyle {
        switch self {
        case .left: return .address
        default: return .default
        }
    }
}

protocol SEFilterHeaderDelegate {
    func showFilterMenu(_ header: SEFilterHeader, didClicked sender: UIButton)
}

@IBDesignable class SEFilterHeader: UIView {
    
    let btnTag = 1000
    
    var delegate: SEFilterHeaderDelegate?

    var view: UIView!
    
    @IBOutlet weak var leftButton: UIButton!
    @IBOutlet weak var centerButton: UIButton!
    
    private var currentSelectedBtn: UIButton?
    
    @IBInspectable var leftTitle: String? {
        get { leftButton.title(for: .normal) }
        set { leftButton.setTitle(newValue, for: .normal) }
    }
    
    @IBInspectable var centerTitle: String? {
        get { centerButton.title(for: .normal) }
        set { centerButton.setTitle(newValue, for: .normal) }
    }
    
    // MARK: - life cycle
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    
    private func setupUI() {
        view = loadViewFromNib()
    }
    
    // MARK: - load nib for view
    
    func loadViewFromNib() -> UIView {
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: nibName(), bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView

        view.frame = bounds
        view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        addSubview(view)

        return view
    }

    private func nibName() -> String {
        return String(describing: type(of: self))
    }
    
    private func _animation(_ sender: UIButton, animate: Bool) {
        sender.isSelected = animate
        currentSelectedBtn = animate ? sender : nil
        UIView.animateKeyframes(withDuration: 0.25, delay: 0, options: .calculationModeLinear) {
            if animate {
                sender.imageView?.transform = CGAffineTransform(rotationAngle: .pi)
            } else {
                sender.imageView?.transform = .identity
            }
        }
    }
    
    // MARK: - action event
    
    @IBAction func buttonAction(_ sender: UIButton) {        
        guard let _currentSelectedBtn = currentSelectedBtn else {
            _animation(sender, animate: true)
            delegate?.showFilterMenu(self, didClicked: sender)
            return
        }
        
        if _currentSelectedBtn == sender {
            _animation(sender, animate: false)
            delegate?.showFilterMenu(self, didClicked: sender)
        } else {
            _animation(_currentSelectedBtn, animate: false)
            delegate?.showFilterMenu(self, didClicked: _currentSelectedBtn)
            
            _animation(sender, animate: true)
            delegate?.showFilterMenu(self, didClicked: sender)
        }
    }
    
    func reset(_ sender: UIButton) {
        _animation(sender, animate: false)
    }
}
